// var comm = { 
	// batch: function (isDate, selectData) {
	function batch (isDate, isBatch, selectData, callback) {
		var returnBatch;
		var selectData = selectData;
		var isDate = isDate;
		var formStr = '';
		var batchStr = '';
		var dateStr = '';
		var batchVal, batchStart, batchEnd;
        var batch_value = [];
		if (isBatch != 0) {
			if (selectData == null || selectData == '') {  // 为下拉框
				batchStr += '<div class="form-group"> <label for="batchInput" class="col-sm-2 col-sm-offset-1 control-label">批次号</label> <div class="col-sm-8"> <input type="text" value="" class="form-control required" id="batchInput" required placeholder="请输入批次号"> </div> </div>';
			}
			else {
				var optionStr = '';
				for (var i = 0; i <= selectData.length - 1; i++) {
					optionStr += '<option value="' + selectData[i].batch_code +'">' + selectData[i].batch_code + '</option>';
				}
				batchStr += '<div class="form-group"> <label for="batchInput" class="col-sm-2 col-sm-offset-1 control-label">批次号</label> <div class="col-sm-8"> <select class="form-control" id="batchInput" required> '+ optionStr +' </select> </div> </div>';
			}
			formStr += batchStr;
		}

		if (isDate == 1) {
			if (selectData == null || selectData == '') {
				dateStr += '<div class="form-group"> <label for="startBatch" class="col-sm-2 col-sm-offset-1 control-label">生产日期</label> <div class="col-sm-8"> <input type="text" value="" class="form-control required" id="startBatch" required data-date-format="yyyy-mm-dd"> </div> </div> <div class="form-group"> <label for="endBatch" class="col-sm-2 col-sm-offset-1 control-label">过期日期</label> <div class="col-sm-8"> <input type="text" value="" class="form-control required" id="endBatch" required data-date-format="yyyy-mm-dd"> </div> </div> ';
				formStr += dateStr;
			}
			else {
				selectData[0].batch_code = selectData[0].batch_code == null ? '' : selectData[0].batch_code;
				var timeStr = '';
				for (var i = 0; i <= selectData.length - 1; i++) {
					timeStr += '<option value="' + selectData[i].product_date +'">' + selectData[i].product_date + '</option>';
				}
				if (selectData[0].batch_code.length > 0) {
					dateStr += '<div class="form-group"> <label for="startBatch" class="col-sm-2 col-sm-offset-1 control-label">生产日期</label> <div class="col-sm-8"> <input type="text" disabled class="form-control" id="startBatch" required data-date-format="yyyy-mm-dd">';
				}
				else {
					dateStr += '<div class="form-group"> <label for="startBatch" class="col-sm-2 col-sm-offset-1 control-label">生产日期</label> <div class="col-sm-8"> <select class="form-control" id="startBatch"> '+ timeStr +' </select>';
				}
				dateStr += '</div> </div> <div class="form-group"> <label for="endBatch" class="col-sm-2 col-sm-offset-1 control-label">过期日期</label> <div class="col-sm-8"> <input type="text" disabled class="form-control" id="endBatch" required data-date-format="yyyy-mm-dd"> </div> </div> ';
				formStr += dateStr;
				
			}
		}
		var modalStr = '<div class="modal fade" role="dialog" aria-labelledby="gridSystemModalLabel" id="batchModal"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close batchClose" aria-label="Close"><span aria-hidden="true">&times;</span></button> <h4 class="modal-title" id="gridSystemModalLabel">SKU信息</h4> </div> <div class="modal-body" style="min-height:100px;"> <form class="form-horizontal"> ' + formStr + ' </form> </div> <div class="modal-footer text-center"> <button type="button" class="btn btn-default batchClose">取消</button> <button type="button" class="btn btn-primary savaBatch">保存</button> </div> </div> </div> </div>';
		// 判断是否是第一次打开
		if ($('body').find('#batchModal').length <= 0) {
			$('body').append(modalStr);
		}
		if (batch_value.length > 0) {
			batch_value = [];
		}
		$('body #batchModal').modal('show');
		
		$('body #batchModal').on('shown.bs.modal', function () {
			if (isBatch != 0) {
				// 批次号为下拉的设置
				if (selectData == null || selectData == '') {
					getInputFocus();
				}
				else {
					// $('#batchInput').select2({});
					$("#batchInput").chosen();
					if (isDate == 1) {
						selectChange();
				        $('#batchInput').change(function() {
				        	selectChange();
			        	})
					}
				}
			}
		})

		function getInputFocus() {
			$('body #batchInput')[0].focus();
			if(isDate == 1){
				$('body #batchInput').keypress(function(e) {
					e.stopPropagation();
					if (e.which == 13)
						$('body #startBatch')[0].focus();
				})
			}
			else {
				$('body #batchInput').keydown(function(e) {
					e.stopPropagation();
					if (e.which == 13)
						$('body .savaBatch')[0].focus();
				})
			}
		}

		// 选择时候值的改变
		function selectChange() {
			var batchSelect = document.getElementById("batchInput").selectedIndex;
			var i = 0;
	        for(i; i < selectData.length; i++) {
	        	if (i == batchSelect) {
	        		$('#startBatch').val(selectData[i].product_date);
					$('#endBatch').val(selectData[i].expire_date);
					$('body .savaBatch')[0].focus();
					break;
	        	}

	        }
	        if (i == selectData.length) {
	        	$('#startBatch').val('');
				$('#endBatch').val('');
				$('body #startBatch')[0].focus();
	        }
		}
		
		// 日历插件
		if (isDate == 1) {
			if (selectData == null || selectData == '') {
				selectTime();
			}
			else {
				if (selectData[0].batch_code.length > 0) {

				}
				else {
					// $("#startBatch").select2();
					$("#startBatch").chosen();
			        selectEnd();
			        $('#startBatch').change(function() {
			        	selectEnd();
			        })
				}
			}
		}

		function selectTime() {
            var nowTemp = new Date();
		    var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);
		     
		    var checkin = $('body #startBatch').datepicker({
		      	onRender: function(date) {
		        	return date.valueOf() < now.valueOf() ? 'disabled' : '';
		      	}
		    }).on('changeDate', function(ev) {
		      	if (ev.date.valueOf() > checkout.date.valueOf()) {
		        	var newDate = new Date(ev.date)
			        newDate.setDate(newDate.getDate() + 1);
			        checkout.setValue(newDate);
		      	}
		      	checkin.hide();
		      	$('body #endBatch')[0].focus();
		    }).data('datepicker');
		    	var checkout = $('#endBatch').datepicker({
		      	onRender: function(date) {
		        	return date.valueOf() <= checkin.date.valueOf() ? 'disabled' : '';
		      	}
		    }).on('changeDate', function(ev) {
		      	checkout.hide();
		      	$('body .savaBatch')[0].focus();
		    }).data('datepicker');
		}

		// 选择时候值的改变
		function selectEnd() {
			var batchStartTime = document.getElementById("startBatch").selectedIndex;
			var i = 0;
	        for(i; i < selectData.length; i++) {
	        	if (i == batchStartTime) {
					$('#endBatch').val(selectData[i].expire_date);
					$('body .savaBatch')[0].focus();
					break;
	        	}

	        }
		}

		//比较两个日期的大小
		function dateCompare(date1,date2){
			date1 = date1.replace(/\-/gi,"/");
			date2 = date2.replace(/\-/gi,"/");
			var time1 = new Date(date1).getTime();
			var time2 = new Date(date2).getTime();
			if(time1 > time2){
				return 1;
			}else if(time1 == time2){
				return 2;
			}else{
				return 3;
			}
		}

		$('body .batchClose').click(function() {
			var deleteBatch = $('body').find('#batchModal');
			var deleteFade = $('body').find('.modal-backdrop');
			deleteBatch.remove();
			deleteFade.remove();
			// $('body #batchModal').modal('hide');
		})

		$('body .savaBatch').click(function() {
			var noneTime = 0;
			$('#batchModal .required').each(function() {
				var thisVal = $(this).val();
				if (thisVal.length <= 0) {
					noneTime ++ ;
					alert("请补全批次号相关信息");
					return false;
				}
			})
			if (noneTime == 0) {
				getVal();
			}
			else {
				return false;
			}
		})

		function getVal() {
			batch_value = [];
			if (isBatch != 0) {
	        	if (selectData == null || selectData == '') {
	        		batchVal = $('body #batchInput').val();
	        	}
	        	else {
	        		batchVal = $('body #batchInput option:selected').val();
	        	}
	        	var batch_input = {'batch_input': batchVal};
	        	batch_value.push(batch_input);
	        }
	        else {
	        	var batch_input = {'batch_input': ''};
	        	batch_value.push(batch_input); 
	        }
	        if (isDate == 1) {
	        	if (selectData == null || selectData == '') {
	        		batchStart = $('#startBatch').val();
	        	}
	        	else {
	        		if (selectData[0].batch_code.length > 0) {
	        			batchStart = $('#startBatch').val();
	        		}
	        		else {
	        			batchStart = $('#startBatch option:selected').val();
	        		}
	        	}
	        	batchEnd = $('#endBatch').val();
	        	var nowDate = (new Date()).toLocaleDateString();
	        	if (dateCompare(batchStart,batchEnd) == 1) {
	        		alert("生产日期不能早于过期日期");
	        		return false;
	        	}
	        	else {
	        		if (dateCompare(batchEnd,nowDate) != 1) {
	        			alert("过期日期不能小于当前日期");
	        			return false;
	        		}
	        		else {
	        			var batch_start = {'batch_start': batchStart};
			        	batch_value.push(batch_start);
			        	var batch_end = {'batch_end': batchEnd};
			        	batch_value.push(batch_end);
	        		}
	        	}
	        }
	        else {
	        	var batch_start = {'batch_start': ''};
	        	batch_value.push(batch_start);
				var batch_end = {'batch_end': ''};
	        	batch_value.push(batch_end);
	        }
			var deleteBatch = $('body').find('#batchModal');
			var deleteFade = $('body').find('.modal-backdrop');
			deleteBatch.remove();
			deleteFade.remove();
			callback && callback( batch_value );
		}	
	}
// }